<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>小额贷款 - 贷款状态</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/bootstrap/5.3.0/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
    <nav class="navbar navbar-dark bg-primary">
        <div class="container">
            <a class="navbar-brand" href="/">
                <i class="fas fa-money-bill-wave me-2"></i>小额贷款
            </a>
            <div>
                <a href="/loan-apply" class="btn btn-outline-light me-2">申请贷款</a>
                <a href="/repayment" class="btn btn-outline-light me-2">还款</a>
                <a href="#" class="btn btn-outline-light" onclick="logout()">退出</a>
            </div>
        </div>
    </nav>

    <div class="container mt-5">
        <h2 class="mb-4">我的贷款</h2>
        <div id="loanList" class="row">
            <div class="col-12 text-center">
                <div class="spinner-border text-primary" role="status">
                    <span class="visually-hidden">加载中...</span>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.bootcdn.net/ajax/libs/bootstrap/5.3.0/js/bootstrap.bundle.min.js"></script>
    <script>
        async function loadLoans() {
            try {
                const response = await fetch('/api/loan-status');
                const data = await response.json();
                
                if (data.success) {
                    displayLoans(data.loans);
                } else {
                    alert(data.message);
                    if (data.message.includes('登录')) {
                        window.location.href = '/login';
                    }
                }
            } catch (error) {
                console.error('Error:', error);
                document.getElementById('loanList').innerHTML = '<div class="col-12 text-center text-danger">加载失败，请刷新重试</div>';
            }
        }

        function displayLoans(loans) {
            const loanList = document.getElementById('loanList');
            
            if (loans.length === 0) {
                loanList.innerHTML = `
                    <div class="col-12 text-center">
                        <i class="fas fa-inbox fa-3x text-muted mb-3"></i>
                        <h5 class="text-muted">暂无贷款记录</h5>
                        <a href="/loan-apply" class="btn btn-primary mt-3">立即申请</a>
                    </div>
                `;
                return;
            }

            loanList.innerHTML = loans.map(loan => {
                const statusBadge = {
                    'pending': 'warning',
                    'approved': 'success',
                    'rejected': 'danger'
                };
                
                const statusText = {
                    'pending': '审批中',
                    'approved': '已通过',
                    'rejected': '已拒绝'
                };

                return `
                    <div class="col-md-6 mb-4">
                        <div class="card">
                            <div class="card-body">
                                <div class="d-flex justify-content-between align-items-start mb-3">
                                    <h5 class="card-title mb-0">贷款申请</h5>
                                    <span class="badge bg-${statusBadge[loan.status]}">${statusText[loan.status]}</span>
                                </div>
                                <p class="card-text">
                                    <strong>金额：</strong>¥${loan.amount}<br>
                                    <strong>期限：</strong>${loan.period}个月<br>
                                    <strong>用途：</strong>${loan.purpose}<br>
                                    <strong>申请时间：</strong>${new Date(loan.apply_date).toLocaleDateString()}
                                </p>
                                ${loan.status === 'approved' ? `
                                    <a href="/repayment" class="btn btn-primary btn-sm">查看还款计划</a>
                                ` : ''}
                            </div>
                        </div>
                    </div>
                `;
            }).join('');
        }

        async function logout() {
            try {
                await fetch('/logout');
                window.location.href = '/';
            } catch (error) {
                window.location.href = '/';
            }
        }

        // 页面加载完成后获取贷款状态
        document.addEventListener('DOMContentLoaded', loadLoans);
    </script>
</body>
</html>